// AudioQueue.h: interface for the CAudioQueue class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_AUDIOQUEUE_H__C3D94C13_86B0_46C9_8034_5D5D08E4094A__INCLUDED_)
#define AFX_AUDIOQUEUE_H__C3D94C13_86B0_46C9_8034_5D5D08E4094A__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "audiodef.h"
class CAudioQueue  
{
public:
	//CAudioQueue( int maxsize = DEFAULT_MAXQUEUE_SIZE );
	CAudioQueue();
	virtual ~CAudioQueue();
public:
	void EnqueueGrow( XAUDIOPACKET *pkt );
	XAUDIOPACKET* Enqueue( XAUDIOPACKET *pkt ); // if queue max out, pop old one
	XAUDIOPACKET* Dequeue( );
	void	Flush();
	int		GetAvgInputSize()					{ return m_nAvgInputSize; }
	int		GetAvgOutputSize()					{ return m_nAvgOutputSize; }
	int		GetQueueSize()						{ return m_nQueueSize; }
	BOOL	IsActive( )							{ return m_bRun; }
	int		GetType( )							{ return m_nType; }
	void	GetInputTime( SYSTEMTIME *stime )	{ FileTimeToSystemTime( &m_LastCheckTime, stime ); }
	void	GetOutputTime( SYSTEMTIME *stime )	{ FileTimeToSystemTime( &m_LastCheckTime2, stime ); }
	void	EnqueueEvent( DWORD event, int channel );
public:
	int		GetChannel( )						{ return m_nChannel; }
	void	SetChannel( int ch )				{ m_nChannel = ch; }	void	AddReceiver( CAudioQueue *q );
	CAudioQueue* GetReceiver( int i )			{ return (i<m_nReceiverNum) ? m_pxq[i] :  NULL; }
	void	DeleteAllReceiver( )				{ memset( m_pxq, 0, sizeof(CAudioQueue*)*m_nReceiverAllocNum ); m_nReceiverNum=0; }
	int		GetReceiverNum()					{ return m_nReceiverNum; }
	int		GetActiveReceiverNum()				{ return m_nActiveReceiver; }
	void	DistributePacket( XAUDIOPACKET *pkt );
	void	DistributePackets( );

private:
	void	CheckTimeInput( XAUDIOPACKET *pkt );
	void	CheckTimeOutput( XAUDIOPACKET *pkt );

protected:
	HANDLE	m_hQueueEvent;
	BOOL	m_bRun;
	int		m_nType;
private:
	int		m_nChannel;
	int		m_nActiveReceiver;
	int		m_nReceiverNum;
	int		m_nReceiverAllocNum;
	CAudioQueue **m_pxq;

private:
	int		m_nMaxQueueSize;
	int		m_nQueueSize;	
	XAUDIOPACKET	*m_pHead;
	XAUDIOPACKET	*m_pTail;
	CRITICAL_SECTION m_CSQ;

private: // for statistics based on second
	int		m_nAvgInputSize;
	int		m_nAvgOutputSize;
	FILETIME m_LastCheckTime;
	FILETIME m_LastCheckTime2;
};

#endif // !defined(AFX_AUDIOQUEUE_H__C3D94C13_86B0_46C9_8034_5D5D08E4094A__INCLUDED_)
